﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using Rabbit.Logic;
using RabbitShop.Code;
using Raven.Imports.Newtonsoft.Json;

namespace RabbitShop.Action
{
    public partial class UserAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Stream postData = Request.InputStream;
            StreamReader sRead = new StreamReader(postData);
            string postContent = sRead.ReadToEnd();
            sRead.Close();

            if (!string.IsNullOrEmpty(postContent))
            {
                try
                {
                    Customer item = JsonConvert.DeserializeObject<Customer>(postContent);
                    item.Cart = new List<ShopCartItem>();
                    RabbitUse.AddOrUpdateCustomer(Db.Store, item);

                    HttpCookie cok = new HttpCookie(Constant.CookieName);
                    cok.Values.Add(Constant.CookieUname, HttpUtility.UrlEncode(item.Name));
                    cok.Values.Add(Constant.CookieUid, item.Id);
                    cok.Values.Add(Constant.CookieCartNo, "0");
                    cok.Expires = DateTime.Now.AddDays(1);
                    Response.AppendCookie(cok);

                    Response.Write("true");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
            else
            {

                Response.Write("false");
            }
        }
    }
}